/****************************************************************
 *
 * Project: Handle ARM M4 STM32F407VG In 2 Weeks
 * 
 ****************************************************************/

/****************************************************************
 *
 *	Module		: "Source code file" myString.c
 *  Tool		: KeilC ARM 4.22a
 *	Chip		: STM32F407VG
 * 	History		: 9/12/2011
 *	Description	: Day 7
				  + My library to play with string type
				  + Reference to header file "myString.h"
 *				
 *	Author		: Dang Anh Tung			
 *	Description	: www.payitforward.edu.vn
 *				
 *
 ****************************************************************/

 
 /****************************************************************
 * 							IMPORT
 ****************************************************************/
 #include "myString.h"


/*****************************************************************
* 							DEFINE
******************************************************************/


 /****************************************************************
 * 							GLOBAL
 ****************************************************************/


 /****************************************************************
 * 						   FUNCTIONS
 ****************************************************************/


/****************************************************************
** Function name:	    myStrCopy
**
** Descriptions:	    Copy content from SourceStr to DestStr with length & begin at offset	
**
** parameters:			
** Returned value:		None
** 
*****************************************************************/
void myStrCopy(char *pSourceStr,char *pDestStr,uint32_t offset, uint32_t length)
{
	uint32_t index;
	pSourceStr += offset;
	for ( index = offset; index < (offset + length) ; index++)
	{
		*pDestStr = *pSourceStr;
		pDestStr++;
		pSourceStr++;
	}
}


/****************************************************************
** Function name:	    myStrCompare
**
** Descriptions:		Compare content of 2 string: return TRUE if all pair of member is equal
**
** parameters:			Compare content of 2 string: return TRUE if all pair of member is equa
** Returned value:	    True or False
** 
*****************************************************************/

uint32_t myStrCompare( char *pStr1, char *pStr2, uint32_t length)
{
	uint32_t i = 0;
	for( i = 0; i < length; i++)
	{
		if (*pStr1 != *pStr2)
		{
			return FALSE;
		}
		else
		{
			pStr1++;
			pStr2++;
		}
	}
	return TRUE;
}

/****************************************************************
** Function name:	    myStrCombine
**
** Descriptions:		Combine 2 string together strResult = [StrHeader][StrFooter]
**
** parameters:			
** Returned value:		None
** 
*****************************************************************/
void myStrCombine(char *pStrHeader,uint32_t lengthHeader, char *pStrFooter, uint32_t lengthFooter, char *pStrResult)
{
	uint32_t i = 0;
	for ( i = 0; i < (lengthHeader + lengthFooter); i++)
	{
		if( i < lengthHeader)
		{
			*pStrResult	 = *pStrHeader;
			pStrResult++;
			pStrHeader++;		
		}
		else
		{
			*pStrResult	 = *pStrFooter;
			pStrResult++;
			pStrFooter++;			
		}
	}
}

/****************************************************************
** Function name:	    myReal2Str
**
** Descriptions:		Convert real number (float or double) to string 
**                      with iLen & fLen is a length of integer and fractional part
**                      +++ Len string = iLen + fLen + 2
**
** parameters:			i: integer part; f: fractional part
** Returned value:		None
** 
*****************************************************************/

void myReal2Str(REAL r, uint8_t *pStr, uint32_t iLen, uint32_t fLen)
{
    uint32_t i;
    uint32_t ti, tf , ei, ef;
    REAL rtemp;
    uint32_t utemp;

    /* Sign */
    if (r < 0)  
    { 
        *pStr++ = '-'; 
        r = -r;
    }
    else
    {
        *pStr++ = ' ';
    }

    for (i = 0, ei = 1; i < iLen; ei *= 10, i++);
    for (i = 0, ef = 1; i < fLen; ef *= 10, i++);
    utemp = (uint32_t)r;
    rtemp = (r - utemp)*ef;

    ti = utemp % ei;
    tf = (uint32_t)rtemp;

    /* integer part ti*/
    for (i = 0; i < iLen; i++)
    {
        ei /= 10;
        *pStr++ = (ti / ei) % 10 + 0x30;
    }

    /* dot or comma*/
    *pStr++ = '.';

    /* fractional part tf*/
    for (i = 0; i < fLen; i++)
    {
        ef /= 10;
        *pStr++ = (tf / ef) % 10 + 0x30;
    }   
}

/****************************************************************
** Function name:	    myInt2Str
**
** Descriptions:		Convert integer number to string 
**                      with iLen is a length of integer part
**                      +++ len string = iLen + 1
**
** parameters:			i: integer part;
** Returned value:		None 
** 
*****************************************************************/
void myInt2Str(int32_t i, uint8_t *pStr, uint32_t iLen)
{
    uint32_t j, ti, ei;
    
    if ( i < 0)
    {
        *pStr++ = '-';
        i = -i;
    }     
    else
    {
        *pStr++ = ' ';
    }

    for (j = 0, ei = 1; j < iLen; ei *= 10, j++);
    ti = ((uint32_t)i) % ei;
    
    for (j = 0; j < iLen; j++)
    {
        ei /= 10;
        *pStr++ = (ti / ei) % 10 + 0x30;
    }
}
 

/****************************************************************
** Function name:	    myStr2Real
**
** Descriptions:		Convert string to number 
**                      with sLen is a length of string
**
** parameters:			 
** Returned value:		None 
** DIDNOT TEST THIS FUNCTION
*****************************************************************/
void myStr2Real(uint8_t * pStr, uint32_t sLen, REAL *r)
{
    REAL powel;
    uint8_t count = 0 , dot_pos = 0; 
    uint8_t i;
    uint8_t *ptemp;
    ptemp = pStr;

    while( count < sLen)
    {
        if (*ptemp == '.')
        {
            break;
        }
        count++;
    }
    dot_pos = count;

    *r = 0;
	// Sum of integer part
	powel = 1;
	for(i = 0 ; i < dot_pos ; i++)
	{
		*r += ( pStr[dot_pos-1-i] - 0x30 ) * powel;
		powel *= 10;
	}
	
	// Sum of fractional part
	powel = (REAL)0.1;
	for( i = dot_pos + 1 ; i < count ; i++)	
	{
		*r += ( pStr[i] - 0x30) * powel;	
		powel *= (REAL)0.1;
	}	  
}
